#include "../include/header.h"

struct ListNode
{
    int         m_nValue;
    ListNode*   next;
};

const ListNode* CoverList(const ListNode* h1, const ListNode* h2)
{
    if (NULL == h1 || NULL == h2)
        return NULL;

   const ListNode *p1,   *p2;
    p1 = h1;
    p2 = h2;

    int num1, num2;

    while(p1)
    {
        num1++;
        p1 = p1->next;
    }

    while(p2)
    {
        num2++;
        p2 = p2->next;
    }

    p1 = h1;
    p2 = h2;

    if (num1 > num2)
    {
        int distance = num1 - num2;
        while(distance > 0)
        {
            distance --;
            p1 = p1->next;
        }
    }
    if (num1 < num2)
    {
        int distance = num2 - num1;
        while(distance > 0)
        {
            distance--;
            p2 = p2->next;
        }
    }

    bool cover = false;

    while (p1 && p2)
    {
        if (p1 == p2)
        {
            cover = true;
            break;
        }
        p1 = p1->next;
        p2 = p2->next;
    }

    if (cover)
        return p2;
    else
        return NULL;
}


